﻿using System;
using System.Windows.Forms;

namespace homework2
{
    public partial class mainFrame : Form
    {
        private string sno;

        private int rowNow, cellNow;
        public mainFrame(string sno)
        {
            InitializeComponent();
            this.sno = sno;
            this.dataGridView1.DataSource = this.getDataSource(sno);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.richTextBox1.Text = (string) this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                this.cellNow = e.ColumnIndex;
                this.rowNow = e.RowIndex;
            }
            catch (InvalidCastException)
            {
                this.richTextBox1.Text = null;
            }
        }

        private void push_Click(object sender, EventArgs e)
        {
            _dataBaseTool.push(this.sno, this.richTextBox1.Text);
            this.dataGridView1.DataSource = this.getDataSource(sno);
        }

        private void update_Click(object sender, EventArgs e)
        {
            _dataBaseTool.update(this.sno, this.richTextBox1.Text,
                (string) this.dataGridView1.Rows[rowNow].Cells[cellNow].Value);
            this.dataGridView1.DataSource = this.getDataSource(sno);
        }

        private void delete_Click(object sender, EventArgs e)
        {
            _dataBaseTool.delete(this.sno, this.richTextBox1.Text);
            this.dataGridView1.DataSource = this.getDataSource(sno);
        }

        private void mainFrame_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
    }
}